package com.cs2340.taskplusplus;

import java.util.ArrayList;
import java.util.Date;

import junit.framework.Assert;
import android.test.AndroidTestCase;

// tests two MORE methods : filterDate and filterList

public class JUnitAdamPart2 extends AndroidTestCase {
	Account a;
	
    public void testFilterDate() throws Throwable {
		//create accounts and lists for testing purposes
		a = new Account("default");
    	a.deleteAccount("test");
    	a = new Account("test","test","test","test");
		TaskList t = a.getMasterList();
		ArrayList<Doable> results = new ArrayList<Doable>();
		
		//try with empty list
		results = (ArrayList<Doable>) Sorter.filterDate(t.getList(), new Date("12/12/2012"), Sorter.DATEMODE.ON);
		Assert.assertTrue("empty list does not return empty list", results.isEmpty());
		
		//add a task (not done)
		Task task1 = new Task(null, "testName1", "testing testing 1", new Date("12/12/2012"), new ArrayList<Label>(), new Location(0,0));
		t.addTask(task1);
		
		//try WITHOUT a date
		results = (ArrayList<Doable>) Sorter.filterDate(t.getList(), null, Sorter.DATEMODE.ON);
		Assert.assertTrue("no date does not return empty list", results.isEmpty());
		
		//try to get no results
		results = (ArrayList<Doable>) Sorter.filterDate(t.getList(), new Date("12/12/2012"), Sorter.DATEMODE.AFTER);
		Assert.assertTrue("should be empty", results.isEmpty());
		
		//add two more tasks
		Task task2 = new Task(null, "testName2", "testing testing 2", new Date("12/12/2010"), new ArrayList<Label>(), new Location(0,0));
		t.addTask(task2);

		Task task3 = new Task(null, "testName3", "testing testing 3", new Date("12/12/2014"), new ArrayList<Label>(), new Location(0,0));
		t.addTask(task3);

		//test before
		results = (ArrayList<Doable>) Sorter.filterDate(t.getList(), new Date("12/12/2012"), Sorter.DATEMODE.BEFORE);
		Assert.assertTrue("BEFORE: returns wrong number of tasks", results.size() == 1);
		Assert.assertTrue("BEFORE: returns incorrect task", results.get(0) == task2);
		
		//test on
		results = (ArrayList<Doable>) Sorter.filterDate(t.getList(), new Date("12/12/2012"), Sorter.DATEMODE.ON);
		Assert.assertTrue("ON: returns wrong number of tasks", results.size() == 1);
		Assert.assertTrue("ON: returns incorrect task", results.get(0) == task1);
		
		//test after
		results = (ArrayList<Doable>) Sorter.filterDate(t.getList(), new Date("12/12/2012"), Sorter.DATEMODE.AFTER);
		Assert.assertTrue("AFTER: returns wrong number of tasks", results.size() == 1);
		Assert.assertTrue("AFTER: returns incorrect task", results.get(0) == task3);
    }
	
    public void testFilterList() throws Throwable {
		//create accounts and lists for testing purposes
		a = new Account("default");
    	a.deleteAccount("test");
    	a = new Account("test","test","test","test");
		TaskList t = a.getMasterList();
		ArrayList<Doable> results = new ArrayList<Doable>();
		
		//create instance of TaskListScreen for testing
		TaskListScreen taskListScreen = new TaskListScreen();
		
		//set the name of the TaskList that TaskListScreen is filtering from
		//and reset the TaskListScreen as if it were remade
		taskListScreen.setName(t.getName());
		
		//add a task (not done)
		Task task1 = new Task(null, "testName1", "testing testing 1", new Date("12/12/2012"), null, new Location(0,0));
		Label label1 = new Label(t.getTableName(), "label1");
		task1.addLabel(label1);
		t.addTask(task1);
		
		//make a list of labels
		ArrayList<Label> labelList = new ArrayList<Label>();
		labelList.add(label1);
		
		//set filtering variables in TaskListScreen
		taskListScreen.setDateMode(Sorter.DATEMODE.ON);
		taskListScreen.setDate(new Date("12/12/2012"));
		taskListScreen.setDoneMode(Sorter.DONEMODE.INCOMPLETE);
		taskListScreen.setLabels(labelList);
		
		//test filter ALL
		taskListScreen.setSortMode(Sorter.FILTER.ALL);
		taskListScreen.filterList();
		results = (ArrayList<Doable>) taskListScreen.getDisplayList();
		Assert.assertFalse("ALL: list should not be empty", results.isEmpty());
		
		//test filter COMPLETE
		taskListScreen.setSortMode(Sorter.FILTER.COMPLETE);
		taskListScreen.filterList();
		results = (ArrayList<Doable>) taskListScreen.getDisplayList();
		Assert.assertFalse("COMPLETE: list should not be empty", results.isEmpty());
		
		//test filter LABEL
		taskListScreen.setSortMode(Sorter.FILTER.LABEL);
		taskListScreen.filterList();
		results = (ArrayList<Doable>) taskListScreen.getDisplayList();
		Assert.assertFalse("LABEL: list should not be empty", results.isEmpty());
		
		//test filter DATE
		taskListScreen.setSortMode(Sorter.FILTER.DATE);
		taskListScreen.filterList();
		results = (ArrayList<Doable>) taskListScreen.getDisplayList();
		Assert.assertFalse("DATE: list should not be empty", results.isEmpty());
    }
}